package com.hue.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * 自定义效验器,需要实现ConstraintValidator，指定效验注解和效验的参数类型
 */
public class ListValueValidator implements ConstraintValidator<ListValue, Integer> {
    private Set<Integer> valueSet = new HashSet<>();

    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] values = constraintAnnotation.values();
        for (int value : values) {
            valueSet.add(value);
        }
    }

    /**
     *
     * @param value:需要效验的值
     * @param context
     * @return
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return valueSet.contains(value);
    }
}
